package com.company.cloud.tag.modules.app.controller;

import java.util.Map;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.tag.modules.app.controller.req.LabelTypeQuery4PageParam;
import com.company.cloud.tag.modules.base.entity.LabelTypeInfo;
import com.company.cloud.tag.modules.base.service.LabelTypeInfoService;
import com.company.cloud.tag.modules.cache.models.LabelThemeModel;
import com.company.cloud.tag.modules.cache.models.LabelTypeModel;
import com.company.cloud.tag.modules.cache.service.LabelCacheService;
import com.company.cloud.tag.modules.cache.utils.TagConvertUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Login
@RestController
@RequestMapping("/app/label/type")
@Api(tags="App_Label_标签分类")
public class LabelTypeController {

    @Autowired
    private LabelTypeInfoService labelTypeInfoService;
    @Autowired
    private LabelCacheService labelCacheService;

    @PostMapping("/query")
    @ApiOperation(value="查询",response = LabelTypeModel.class)
    public BaseResult query(@RequestBody LabelTypeQuery4PageParam param) {
        IPage<LabelTypeInfo> page = labelTypeInfoService.page(new Page<LabelTypeInfo>(param.getPage(),param.getRows()),
                new QueryWrapper<LabelTypeInfo>()
                        .eq(StringUtils.hasText(param.getTypeNo()),"type_no",param.getTypeNo())
                        .like(StringUtils.hasText(param.getTypeName()),"type_name",param.getTypeName())
                        .eq(StringUtils.hasText(param.getType()),"type",param.getType())
                        .eq(StringUtils.hasText(param.getThemeNo()),"theme_no",param.getThemeNo())
                        .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                        .orderByDesc("orders")
        );
        Map<String, LabelThemeModel> themeMap= labelCacheService.getLabelThemeMapByDB();
        IPage<LabelTypeModel> data = page.convert(new Function<LabelTypeInfo, LabelTypeModel>() {
            @Override
            public LabelTypeModel apply(LabelTypeInfo entity) {
                return TagConvertUtils.getLabelType(entity,themeMap);
            }
        });
        return BaseResult.ok(data);
    }





}
